package com.android.mios.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.android.mios.R;
import com.android.mios.adapter.SystemSwitchSkinGridViewAdapter;
import com.android.mios.model.Engine;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Global {

	private static Global _instance;
	private Context mContext;
	private Map<Integer, Engine> engines;
	private int roomID = -1;
	private int serialNumber = -1;
	private static final String ROOM_PATH = "Smartwave/Room";
	private static final String Skin_PATH = "Smartwave/Skin";
	private LinearLayout mainLayout;
	private SystemSwitchSkinGridViewAdapter adapter;
	private int pos;

	private static final int WIDTH = 1280;
	private static final int HEIGHT = 800;

	public SystemSwitchSkinGridViewAdapter getAdapter() {
		return adapter;
	}

	public void setAdapter(SystemSwitchSkinGridViewAdapter adapter) {
		this.adapter = adapter;
	}

	public int getPos() {
		return pos;
	}

	public void setPos(int pos) {
		this.pos = pos;
	}

	public LinearLayout getMainLayout() {
		return mainLayout;
	}

	public void setMainLayout(LinearLayout mainLayout) {
		this.mainLayout = mainLayout;
	}

	public String getRoomPath() {

		return ROOM_PATH;
	}

	public String getSkinPath() {

		return Skin_PATH;
	}

	public int getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(int serialNumber) {
		this.serialNumber = serialNumber;
	}

	private DisplayMetrics mDisplayMetrics;
	private String myTitle = "我的Smartwave";

	private boolean isRuningWeather = false;

	private Toast toast = null;

	public static Global getInstance(Context context) {

		if (_instance == null)
			_instance = new Global(context);
		return _instance;

	}

	public static Global getInstance() {

		return _instance;
	}

	private Global(Context context) {

		mContext = context;

		engines = new HashMap<Integer, Engine>();

		mDisplayMetrics = context.getResources().getDisplayMetrics();

	}

	public File getFileForPath(String path) {

		File file = new File(Environment.getExternalStorageDirectory()
				+ File.separator + path);
		if (!file.exists())
			file.mkdirs();

		return file;

	}

	public boolean getExistSDCard() {

		if (!Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED))
			return false;
		else
			return true;

	}

	public DisplayMetrics getDisplayMetrics() {

		return mDisplayMetrics;
	}

	public String getMyTitle() {

		return myTitle;

	}

	public boolean getIsRuningWeather() {

		return isRuningWeather;
	}

	public void setIsRuningWeather(boolean isRuningWeather) {

		this.isRuningWeather = isRuningWeather;
	}

	public void setRoomID(int roomID) {

		this.roomID = roomID;
	}

	public int getRoomID() {

		return roomID;
	}


	public Map<Integer, Engine> getEngines() {

		return engines;
	}

	public void setEngines(Map<Integer, Engine> engines) {

		this.engines = engines;
	}

	public List<Engine> findMiosEngines() {

		List<Engine> miosEngines = new ArrayList<Engine>();

		for (Engine engine : engines.values()) {
			if (!engine.isAccessories()) {
				miosEngines.add(engine);
			}
		}
		return miosEngines;
	}

	public void showAlert(Context context, String title, String message) {

		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title)
		.setMessage(message)
				.setCancelable(true)
				.setPositiveButton(
						context.getResources().getString(
								com.android.mios.R.string.ok),
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.dismiss();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();

	}

	public void showTextToast(Context context, String msg) {

		if (toast == null) {
			toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
		} else {
			toast.setText(msg);
		}

		toast.show();
	}


	public int completeSize(int width, int height) {

		int widthPl = (int) (width / WIDTH);
		if (widthPl <= 0)
			widthPl = 1;

		int heightPl = (int) (height / HEIGHT);
		if (heightPl <= 0)
			heightPl = 1;

		return Math.min(widthPl, heightPl);

	}

	public void setCurrentUserBg(Resources res, String currentUserBg,
			LinearLayout layout) {

		if (currentUserBg.equals("bg_0"))
			layout.setBackgroundDrawable(res.getDrawable(R.drawable.bg_0));
		else if (currentUserBg.equals("bg_1"))
			layout.setBackgroundDrawable(res.getDrawable(R.drawable.bg_1));
		else if (currentUserBg.equals("bg_2"))
			layout.setBackgroundDrawable(res.getDrawable(R.drawable.bg_2));
		else if (currentUserBg.equals("bg_3"))
			layout.setBackgroundDrawable(res.getDrawable(R.drawable.bg_3));
		else {
			if (Global.getInstance().getExistSDCard()) {

				File file = new File(Global.getInstance().getFileForPath(
						Global.getInstance().getSkinPath()), currentUserBg);
				if (file != null) {

					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inJustDecodeBounds = true;

					BitmapFactory.decodeFile(file.getPath(), options);

					options.inJustDecodeBounds = false;

					options.inSampleSize = completeSize(options.outWidth,
							options.outHeight);

					Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(),
							options);
					
					if (bitmap != null)
						layout.setBackgroundDrawable(Utils
								.bitmapToDrawableByBD(bitmap));
				}
			}
		}
	}
}
